home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / which.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  1KB  |  79 lines

  1. /* which - search paths for executable */
  2.  
  3. #define DELIMITER ':'
  4.  
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8.  
  9. int main(ac, av)
  10. char **av;
  11. {
  12.   char *path, *cp;
  13.   char buf[400];
  14.   char prog[400];
  15.   char patbuf[512];
  16.   int quit, none;
  17.  
  18.   if (ac < 2) {
  19.     fprintf(stderr, "Usage: %s cmd [cmd, ..]\n", *av);
  20.     exit(1);
  21.   }
  22.   av[ac] = 0;
  23.   for (av++; *av; av++) {
  24.  
  25.     quit = 0;
  26.     none = 1;
  27.     if ((path = getenv("PATH")) == NULL) {
  28.         fprintf(stderr, "Null path.\n");
  29.         exit(0);
  30.     }
  31.     strcpy(patbuf, path);
  32.     path = patbuf;
  33.     cp = path;
  34.  
  35.     while (1) {
  36.         cp = strchr(path, DELIMITER);
  37.         if (cp == NULL)
  38.             quit++;
  39.         else
  40.             *cp = '\0';
  41.  
  42.         if (strcmp(path, "") == 0 && quit == 0) {
  43.             sprintf(buf, "%s./%s", path, *av);
  44.         } else
  45.             sprintf(buf, "%s/%s", path, *av);
  46.  
  47.         /* Fprintf(stderr,"Trying %s, path %s\n",buf,path); */
  48.  
  49.         path = ++cp;
  50.  
  51.         if (access(buf, 1) == 0) {
  52.             printf("%s\n", buf);
  53.             none = 0;
  54.         }
  55.         sprintf(prog, "%s.%s", buf, "prg");
  56.         if (access(prog, 1) == 0) {
  57.             printf("%s\n", prog);
  58.             none = 0;
  59.         }
  60.         sprintf(prog, "%s.%s", buf, "ttp");
  61.         if (access(prog, 1) == 0) {
  62.             printf("%s\n", prog);
  63.             none = 0;
  64.         }
  65.         sprintf(prog, "%s.%s", buf, "tos");
  66.         if (access(prog, 1) == 0) {
  67.             printf("%s\n", prog);
  68.             none = 0;
  69.         }
  70.         if (quit) {
  71.             if (none)
  72.                 printf("No %s in %s\n", *av, getenv("PATH"));
  73.             break;
  74.         }
  75.     }
  76.   }
  77.   exit(0);
  78. }
  79.